<?php

namespace application\member\controller;

use ticky\controller;
use ticky\captcha;

class register extends controller {

    /**
     * 会员注册
     */
    public function index() {
        $config = get_config();
        if (isset($_POST['dosubmit'])) {
            captcha::instance()->check_code();
            $member = db('member');
            $data = array();
            $data['username'] = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmsg(L('user_name_format_error'));
            $data['password'] = isset($_POST['password']) && is_password($_POST['password']) ? trim($_POST['password']) : showmsg(L('password_format_error'));

            if ($config['register']['regverify'] == '1') {
                $data['email'] = isset($_POST['email']) && is_email($_POST['email']) ? trim($_POST['email']) : showmsg(L('mail_format_error'));
                $data['email_status'] = $config['mail_open'] == '1' ? '1' : '0';
                $data['status'] = '1';
            } elseif ($config['register']['regverify'] == '2') {
                $data['phone'] = isset($_POST['phone']) && is_email($_POST['phone']) ? trim($_POST['phone']) : showmsg(L('phone_format_error'));
                $data['phone_status'] = '0';
                $data['status'] = '1';
            } elseif ($config['register']['regverify'] == '3') {
                $data['status'] = '0';
            } else {
                $data['status'] = '1';
            }

            $result = $member->where(array('username' => $_POST['username']))->find();
            if ($result) {
                showmsg('该用户已存在！');
            }
            $result = $member->where(array('email' => $_POST['email']))->find();
            if ($result) {
                showmsg("该邮箱已存在！");
            }
            $result = $member->where(array('phone' => $_POST['phone']))->find();
            if ($result) {
                showmsg("该手机号码已存在！");
            }

            $data["password"] = password($data['password']);
            $data['regdate'] = $data['lastlogintime'] = TIMESTAMP;
            $data['regip'] = $data['lastloginip'] = getip();
            $data['loginnum'] = '1';
            $data['groupid'] = '1';
            $data['amount'] = '0.00';
            $data['adminid'] = '0';
            $data['point'] = $data['experience'] = $config['member_point'];  //经验和积分
            $data['dateline'] = TIMESTAMP;
            $data['uid'] = $member->insert($data, true);
            if (!$data['uid']) {
                showmsg("注册失败！");
            }
            session('uid', $data['uid']);
            session('username', $data['username']);
            session('groupid', $data['groupid']);
            session('adminid', $data['adminid']);
            showmsg('注册成功！', url('member/spacecp/index'), 1);
        } else {
            $this->assign('config', $config);
            $this->assign('title', '会员注册');
            $this->display('register');
        }
    }

}
